﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluginInterface;

namespace Buccontrols
{
    public class PluginActionCollection
    {
        Dictionary<string, PluginAction> actions;
        int lastLenght = 0;

        public PluginActionCollection()
        {
            this.actions = new Dictionary<string, PluginAction>();
            this.lastLenght = 0;
        }

        public void Register(PluginAction action)
        {
            this.actions.Add(action.name, action);
            this.lastLenght = this.actions.Count;
        }

        public void Remove(string actionName)
        {
            this.actions.Remove(actionName);
            this.lastLenght = this.actions.Count;
        }

        public ActionResult Execute(string action, string[] args)
        {
            return this.actions[action].Execute(args);
        }

        public Dictionary<string, PluginAction> Actions
        {
            get
            {
                if (this.actions.Count != this.lastLenght)
                {
                    foreach (string s in this.actions.Keys)
                    {
                        if (s != this.actions[s].name)
                        {
                            try { this.actions.Add(this.actions[s].name, this.actions[s]); }
                            catch { }
                            this.actions.Remove(s);
                        }
                    }
                    this.lastLenght = this.actions.Count;
                }
                return this.actions;
            }
            set
            {
                this.actions = value;
            }
        }
    }
}
